/*
 * Created by SharpDevelop.
 * User: fatman
 * Date: 10/27/2008
 * Time: 3:43 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using CUALib;
using WF=System.Windows.Forms;
using System.Drawing;

namespace CUALib
{
	/// <summary>
	/// Description of CUAWinFormsFactory.
	/// </summary>
	//public class CUAWinFormsFactory
	
	public static class CUAWinFormsFactory
	{
		public static Window CreateFromWindowsForm(Type formType, int redFact)
		{

			if (redFact <= 0)
				redFact = 1;
			
			Window w = new Window(true);
			WF.Form f = (WF.Form) formType.Assembly.CreateInstance(formType.FullName);
			RecurseContainer(f, w, redFact);
			

			//Console.SetWindowSize(f.Size.Width / redFact, f.Size.Height / redFact);
			//Console.SetBufferSize(f.Size.Width / redFact, f.Size.Height / redFact);

			w.Size = new Size(f.Size.Width / redFact, f.Size.Height / redFact);

			return w;
		}

		private static void RecurseContainer(WF.Control control, ConsoleControl consoleControl, int redFact)
		{
			bool cons = !false;
			//Console.WriteLine("\n\n{0}:{1} = {2}:{3}",control.GetType().Name, control.Name, consoleControl.GetType().Name, consoleControl.Name);

			foreach (WF.Control c in control.Controls)
			{
				
				//Console.WriteLine("\t{0} {1} {2} {3}", control.GetType().Name, c.GetType().Name, consoleControl.GetType().Name, redFact);
				ConsoleControl cc = null;
				if (c is WF.Button)
				{
					cc = new Button(cons);
					((Button) cc).Text = c.Text;
					//((Button) cc).OnClick += new ButtonOnClick(WindowFactory_OnClick);
					
				}
				else if (c is WF.GroupBox || c is WF.Panel)
				{
					cc = new Panel(cons);
					((Panel) cc).Title = c.Text;
				}
				else if (c is WF.TextBox)
				{
					cc = new TextBox(cons);
					((TextBox) cc).Text = c.Text;
				}
				if (cc != null)
				{
					//Console.WriteLine("\t{0}", c);
					cc.Name = c.Name;
					cc.Size = new Size(2 + c.Size.Width / redFact, 2 + c.Size.Height / redFact);
					//cc.Location = new Point(consoleControl.Location.X + c.Location.X / redFact, consoleControl.Location.Y + c.Location.Y / redFact);
					cc.Location = new Point( c.Location.X / redFact,  c.Location.Y / redFact);
					consoleControl.Controls.Add(cc);
					RecurseContainer(c, cc, redFact);
				}

			}

		}

		static void WindowFactory_OnClick(object sender, EventArgs e)
		{
			//Console.WriteLine("CLICK " +sender);
		}
	}

}
